package com.seavenois.alife2;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

/*************************************************/
/* An activity to set preferences ****************/
/* Its a menu that saves SharedPreferences. ******/
/*************************************************/
public class PrefsFragment extends PreferenceFragment {

	ListPreference styleSelector;			//List for choosing graphics style
	SharedPreferences mySharedPreferences;	//Stored preferences
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	  
		// Load the preferences from an XML resource
		addPreferencesFromResource(R.xml.menu_settings);
		 
		//Assign elements
		styleSelector = (ListPreference) findPreference("settings_graphicStyle");
		
		//Initial values
		mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
		updateStyleSummary(0);
	    styleSelector.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

	    	@Override
	    	public boolean onPreferenceChange(Preference preference, Object newValue) {
	    		updateStyleSummary(newValue);
	    		return true;
	    	}

	    });
	}

	//Update style selector summary
	public void updateStyleSummary(Object newValue){
		int stylePreference = Integer.parseInt(newValue.toString());
		switch (stylePreference){
			case 0:
				styleSelector.setSummary(getString(R.string.settings_menu_styleCartoon));
				break;
			case 1:
				styleSelector.setSummary(getString(R.string.settings_menu_styleReal));
				break;
		}
	}
}